import axios from 'axios'
import {warningMessage} from "@/utils/message";

const selfInterceptorsResponse=function (response){
    if(requestIsSuccess(response)){
        return response;
    }else{
        warningMessage(requestGetServerMessage(response))
        return response;
    }
}
const selfInterceptorsError=function (error){
    // warningMessage(this,error.message)
    // alert(error.message)
    warningMessage(requestGetError(error))
    return error.response;
}

var requests = axios.create({
    baseURL:'api',
})
var requestsLongtime = axios.create({
    baseURL:'api',
    timeout:50000
})
const requestsWithoutToken = axios.create({
    baseURL:'api',
    timeout:5000,
})
const amap = axios.create({
    baseURL:"https://restapi.amap.com/v3",
    params:{
        key:'8c895014d17b199ba982198ca1c53a10',
        coordsys:'gps'
    }
})


requests.interceptors.response.use(selfInterceptorsResponse,selfInterceptorsError)


requestsLongtime.interceptors.response.use(selfInterceptorsResponse,selfInterceptorsError)
// requests.interceptors.response.use(selfInterceptorsResponse,selfInterceptorsError)
requestsWithoutToken.interceptors.response.use(selfInterceptorsResponse,selfInterceptorsError)
const requestIsSuccess=(result)=>{
    return result.status==200&&result.data.state==200
}
const requestGetData=(result)=>{
    return result.data.data
}
const requestGetError=(error)=>{
    return error.config.url+" "+error.message;
}
const requestGetServerMessage=(result)=>{
    return result.data.msg
}
export {requests,amap,requestsWithoutToken,requestsLongtime,requestIsSuccess,requestGetData};